package com.xh.petdogexpert.sys.service.imp;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.sys.dto.LovItem;
import com.xh.petdogexpert.sys.mapper.LovItemMapper;
import com.xh.petdogexpert.sys.service.ILovItemService;


@Service
@Transactional
public class LovItemServiceImp implements ILovItemService {

	@Autowired
	LovItemMapper LovItemMapper;
	private Logger logger = LoggerFactory.getLogger(LovItemServiceImp.class);
	
	@Override
	public LovItem getLovItem(LovItem lovItem) throws BaseException {
		List<LovItem> lovs = LovItemMapper.queryLovItems(lovItem);
		if (lovs.size() > 1) {
			throw new BaseException("获取多于一个LOV_ITEM的数据");
		} else if (lovs == null || lovs.size() == 0) {
			throw new BaseException("不存在此LOV_ITEM的数据");
		} else {
			return lovs.get(0);
		}

	}
	
	public List<LovItem> getLovItems(LovItem lovItem) { 
		return LovItemMapper.queryLovItems(lovItem);
	}
	
	@Override
	public void addUpdateLovItem(List<LovItem> lovItems) throws BaseException {
		try {
			for(LovItem lovItem : lovItems) {
			if (lovItem.getId() != null)
				LovItemMapper.updateLovItem(lovItem);
			else
				LovItemMapper.addLovItem(lovItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("保存出错");
		}
		
	}

	@Override
	public int deleteLovItem(LovItem lovItem) throws BaseException {
		int count;
		try {
			count = LovItemMapper.deleteLovItem(lovItem);

		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("删除失败");
		}
		return count;
	}

	@Override
	public List<LovItem> queryLovItems(LovItem lovItem) {
		List<LovItem> lovs = LovItemMapper.queryLovItems(lovItem);
		return lovs;
	}
	
}
